package com.wrl.mmall.controller.portal;

import com.wrl.mmall.common.Const;
import com.wrl.mmall.common.ResponseCode;
import com.wrl.mmall.common.ServerResponse;
import com.wrl.mmall.pojo.User;
import com.wrl.mmall.service.ICartService;
import com.wrl.mmall.util.CookieUtil;
import com.wrl.mmall.util.JsonUtil;
import com.wrl.mmall.util.RedisShardedPoolUtil;
import com.wrl.mmall.vo.CartVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 购物车管理
 * Created by Wang Renlei on 2018/9/20.
 */
@Slf4j
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private ICartService iCartService;

    /**
     *  查看自己的购物车，只需要登录即可
     * @param
     */
    @RequestMapping(value="list.do")
    @ResponseBody
    public ServerResponse<CartVo> list(HttpServletRequest httpServletRequest){
        log.info("CartController.list");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }

        return iCartService.list(user.getId());
    }

    /**
     *  添加到购物车
     * @param productId 添加物品的id
     * @param count     添加多少个
     */
    @RequestMapping(value="add.do")
    @ResponseBody
    public ServerResponse<CartVo> add(HttpServletRequest httpServletRequest, Integer productId, Integer count){
        log.info("CartController.add");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.add(user.getId(), productId, count);
    }

    /**
     * 更新购物车
     * @param productId 需要更新的物品id
     * @param count     更改为多少个
     */
    @RequestMapping(value="update.do")
    @ResponseBody
    public ServerResponse<CartVo> update(HttpServletRequest httpServletRequest, Integer productId, Integer count){
        log.info("CartController.update");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.update(user.getId(), productId, count);
    }

    /**
     *  删除购物车中的商品
     * @param productIds    需要删除的物品id，可以是多个，中间以逗号分割
     */
    @RequestMapping(value="delete.do")
    @ResponseBody
    public ServerResponse<CartVo> delete(HttpServletRequest httpServletRequest, String productIds){
        log.info("CartController.delete");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.deleteProduct(user.getId(), productIds);
    }


    /**
     * 购物车，物品全选
     * @param
     * @return
     */
    @RequestMapping(value="select_all.do")
    @ResponseBody
    public ServerResponse<CartVo> selectAll(HttpServletRequest httpServletRequest){
        log.info("CartController.selectAll");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.selectOrUnSelect(user.getId(),null, Const.Cart.CHECKED);
    }

    /**
     * 购物车，物品反选
     * @param
     * @return
     */
    @RequestMapping(value="un_select_all.do")
    @ResponseBody
    public ServerResponse<CartVo> unSelectAll(HttpServletRequest httpServletRequest){
        log.info("CartController.unSelectAll");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.selectOrUnSelect(user.getId(),null, Const.Cart.UN_CHECKED);
    }


    /**
     * 购物车，单选
     * @param
     * @param productId
     * @return
     */
    @RequestMapping(value="select.do")
    @ResponseBody
    public ServerResponse<CartVo> select(HttpServletRequest httpServletRequest, Integer productId){
        log.info("CartController.select");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.selectOrUnSelect(user.getId(), productId, Const.Cart.CHECKED);
    }

    /**
     * 购物车，单独反选
     * @param
     * @param productId
     * @return
     */
    @RequestMapping(value="un_select.do")
    @ResponseBody
    public ServerResponse<CartVo> unSelect(HttpServletRequest httpServletRequest, Integer productId){
        log.info("CartController.unSelect");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.selectOrUnSelect(user.getId(), productId, Const.Cart.UN_CHECKED);
    }

    /**
     * 查询当前用户购物车中的产品数量。
     * @return
     */
    @RequestMapping(value="get_cart_product_count.do")
    @ResponseBody
    public ServerResponse<Integer> getCartProductCount(HttpServletRequest httpServletRequest){
        log.info("CartController.getCartProductCount");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null){
            return  ServerResponse.createBySuccess(0);
        }
        return iCartService.getCart(user.getId());
    }

















}
